<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    {% block css %}

    {% endblock %}
</head>
<body>
<!--导航条开始-->
<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/home/">
          {% block title %}
                BBS
          {% endblock %}
      </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
          {% if request.user.is_authenticated %}
                <li style="border: 3px solid white;display: inline-block;height: 40px;border-radius: 50%;width: 40px;margin-top: 5px;overflow: hidden">
                    <img src="/media/{{ request.user.avatar }}" alt="" style="max-width: 100%">
                </li>
                <li><a href="#">{{ request.user.username }}</a></li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多操作 <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <li><a href="#" data-toggle="modal" data-target="#myModal">修改密码</a></li>
                    <li><a href="#" data-toggle="modal" data-target="#myModal1">修改头像</a></li>
                    <li><a href="/backend/">后台管理</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="/logout/">注销登录</a></li>
                  </ul>
                </li>
              {% else %}
                <li><a href="/register/">注册</a></li>
                <li><a href="/login/">登录</a></li>
          {% endif %}
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">

      <div class="modal-body">
        <h2 class="text-center">修改密码</h2>
          <form id="form">
              {% csrf_token %}
              <div class="form-group">
                  <label for="">用户名:</label>
                  <input type="text" value="{{ request.user.username }}" disabled class="form-control">
              </div>
              <div class="form-group">
                  <label for="">旧密码:</label>
                  <input type="text" name="old_password" class="form-control">
              </div>
              <div class="form-group">
                  <label for="">新密码:</label>
                  <input type="text" name="new_password" class="form-control">
              </div>
              <div class="form-group">
                  <label for="">确认密码:</label>
                  <input type="text" name="confirm_password" class="form-control">
              </div>
              <input type="button" value="修改密码" id="subBtn" class="btn btn-warning btn-block">
          </form>
      </div>
      <div class="modal-footer">
          <span style="color: red" id="error"></span>
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary">确认</button>
      </div>
    </div>
  </div>
</div>
<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">

      <div class="modal-body">
        <h2 class="text-center">修改头像</h2>
          <form id="form" action="/update/avatar/" method="post" enctype="multipart/form-data">
              {% csrf_token %}
              原头像:
              <div class="form-group" style="height: 200px;width: 200px;border-radius: 50%;overflow: hidden">
                    <img src="/media/{{ request.user.avatar }}" alt="" style="max-width: 100%">
              </div>
              <div class="form-group" >
                  新头像:
                    <p><label for="avatar">
                        <img src="/static/img/default.jpg" alt="" width="120" id="img">
                    </label></p>
                    <input type="file" name="avatar" id="avatar" style="display: none">
                </div>  <!--该div仅仅是增加了上下两个标签的距离 更好看一些-->
              <input type="submit" value="修改头像" id="subAvatar" class="btn btn-danger btn-block">
          </form>
      </div>
      <div class="modal-footer">
          <span style="color: red" id="error"></span>
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary">确认</button>
      </div>
    </div>
  </div>
</div>
<!--导航条结束-->

<div class="container-fluid">
    <div class="row">
        <div class="col-md-2">
        <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
          <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingOne">
              <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                  博客后台
                </a>
              </h4>
            </div>
            <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
              <div class="panel-body">
                  <p><a href="/add/article/">添加文章</a></p>
                  <p><a href="#">博客签名</a></p>
                  <p><a href="#">博客备份</a></p>
                  <p><a href="#">更多操作</a></p>
              </div>
            </div>
          </div>
          <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingTwo">
              <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                  文章分类
                </a>
              </h4>
            </div>
            <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
              <div class="panel-body">
                    <p><a href="#">添加分类</a></p>
                    <p><a href="#">管理分类</a></p>
              </div>
            </div>
          </div>
          <div class="panel panel-default">
            <div class="panel-heading" role="tab" id="headingThree">
              <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                    文章标签
                </a>
              </h4>
            </div>
            <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
              <div class="panel-body">
                     <p><a href="#">添加标签</a></p>
                     <p><a href="#">管理标签</a></p>
              </div>
            </div>
          </div>
        </div>

    </div>
        <div class="col-md-10">
        <div>

          <!-- Nav tabs -->
          <ul class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">文章</a></li>
            <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">链接</a></li>
            <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">相册</a></li>
            <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">设置</a></li>
          </ul>

          <!-- Tab panes -->
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane active" id="home">
                {% block article %}

                {% endblock %}

            </div>
            <div role="tabpanel" class="tab-pane" id="profile">链接操作</div>
            <div role="tabpanel" class="tab-pane" id="messages">相册管理</div>
            <div role="tabpanel" class="tab-pane" id="settings">设置中心</div>
          </div>

        </div>

    </div>
    </div>
</div>



<script>
    $('#subBtn').click(function () {
        let dataDict = {};
        $.each($('#form').serializeArray(),function (index,dictObj) {
            dataDict[dictObj.name] = dictObj.value
        })
        $.ajax({
            url:'/set_pwd/',
            type:'post',
            data:dataDict,
            success:function (args) {
                if (args.code === 10000){
                    window.location.href = args.url
                }else{
                    $('#error').text(args.msg)
                }
            }
        })
    })

    $('.delBtn').click(function () {
        let isDel = confirm('你确定要删除吗???')
        let $delBtn = $(this)
        if (isDel){
            $.ajax({
                url:'/delete/article/',
                type:'get',
                data:{'delete_id':$delBtn.attr('delete_id')},
                success:function (args) {
                    if(args.code === 10000){
                        $delBtn.parent().parent().remove()
                    }
                }
            })
        }
    })
    // 用户头像实时展示
    $('#avatar').change(function () {
        // 1.产生一个文件阅读器对象
        let myFileReader = new FileReader();
        // 2.获取用户上传的头像文件
        let avatarObj = this.files[0];
        // 3.将文件对象交给阅读器加载
        myFileReader.readAsDataURL(avatarObj)  // IO操作 需要消耗时间 但是是异步
        // 4.修改img标签的src属性
        // 等待文件阅读器对象加载完毕之后再修改src属性
        myFileReader.onload = function(){
            $('#img').attr('src',myFileReader.result)
        }
    })
</script>
{% block js %}

{% endblock %}
</body>
</html>